package com.example.demo.user;

import com.example.demo.model.CommonResponse;
import com.example.demo.model.UserBean;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * Created by 豆腐干Sama on 2018/5/8.
 */
@RestController
public class UserController {
    @GetMapping("api/getUserData")
    public CommonResponse getUserData() {
        CommonResponse response = new CommonResponse();
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            response.succeed(principal);
        } else {
            response.failed();
        }
        return response;
    }
    @PostMapping("api/user")
    public UserBean user(@RequestBody UserBean user) {
        return user;
    }
}
